<template>
    <div>
        <el-main class="main">
            <!-- 多条件搜索 -->
            <el-form :inline="true" :model="formData" class="demo-form-inline">
                <el-form-item label="订单编号">
                    <el-select v-model="formData.orderid" placeholder="请选择" style="width: 180px">
                        <el-option v-for="item in allno" :key="item.id" :label="item.orderno" :value="item.id">
                        </el-option>
                    </el-select></el-form-item>
                <el-form-item label="次品出库单编号">
                    <el-input v-model="formData.no" placeholder="次品处理单编号" style="width: 180px"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="formData.status" placeholder="请选择" style="width: 180px">
                        <el-option label="未审核" value="0"></el-option>
                        <el-option label="已审核" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <!-- 按钮 -->
                <el-button type="primary" @click="find">查询</el-button>
                <el-button type="warning" @click="empty">重置</el-button>
            </el-form>
            <el-col style="text-align: left">
                <el-button type="primary" @click="openAdd">添加次品处理单</el-button>
            </el-col>
            <!-- 列表 -->
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column prop="no" label="处理单单号" width="180">
                </el-table-column>
                <el-table-column prop="orderid" label="订单编号" width="180">
                </el-table-column>
                <el-table-column prop="time" label="出库时间" width="180">
                </el-table-column>
                <el-table-column prop="status" label="状态" width="180">
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" plain @click="minute(scope.row.orderid)">查看详情</el-button>
                        <el-button v-if="scope.row.status == 1" type="warning" plain
                            >已审核</el-button>
                        <el-button v-if="scope.row.status == 0" type="success" plain
                            @click="pass(scope.row)">审核</el-button>
                        <el-button v-if="scope.row.status == 0" type="danger" plain @click="deleteItem(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-row>
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="page" :page-sizes="[5, 10]" :page-size="size"
                    layout=" prev, pager, next, jumper,total, sizes" :total="total">
                </el-pagination>
            </el-row>

            <!-- 添加表单 -->
            <el-dialog title="添加次品发货单" :visible.sync="dialogVisible" width="35%">
                <div>
                    <el-form label-width="80px" :model="formData">
                        <el-form-item label="请选择订单">
                            <el-select v-model="formData.no" clearable placeholder="请选择" style="width: 320px"
                                @change="changeing">
                                <el-option v-for="item in orderNo" :key="item.id" :label="item.orderno" :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                    <el-button type="text" @click="isMinute">订单详情</el-button>
                    <el-button type="primary" @click="add">添加</el-button>
                </div>
            </el-dialog>

            <!-- 查看详情页 -->
            <el-dialog title="订单详情" :visible.sync="minuteVisible" width="80%">
                <div style="width: 90%;">
                    <el-table :data="goodsData" border style="width: 100%" @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="55">
                        </el-table-column>
                        <el-table-column prop="id" label="序号" width="180">
                        </el-table-column>
                        <el-table-column prop="productid" label="商品名" width="180">
                        </el-table-column>
                        <el-table-column prop="num" label="数量" width="180">
                        </el-table-column>
                        <el-table-column prop="unit" label="单位" width="180">
                        </el-table-column>
                        <el-table-column prop="status" label="状态" width="180">
                        </el-table-column>
                        <el-table-column label="操作" v-if="ifMinute == 1">
                            <template slot-scope="scope">
                                <el-button type="primary" plain v-if="scope.row.status == 0"
                                    @click="sendPass(scope.row)">发货</el-button>
                                <el-button v-if="scope.row.status == 1" type="warning" plain
                                    >已发货</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-button type="primary" @click="allSend" v-if="ifMinute == 1">一键发货</el-button>
                </div>
            </el-dialog>
        </el-main>
    </div>
</template>
<script>
export default {
    data() {
        return {
            tableData: [],
            goodsData: [],
            //多条件查询
            formData: {
                id: '',
                no: '',
                orderid: '',
                status: '',
                deleted: '',
                time: '',
            },
            oldForm: {},
            dialogVisible: false,
            minuteVisible: false,
            page: 1,
            size: 5,
            total: 0,
            totalPage: 0,
            warehouse: [],
            no: [],
            allno: [],
            employee: [],
            multipleSelection: [],
            oid: 0,
            status: [],
            ifMinute: null,
            orderNo: [],
        }
    }, mounted() {
        //查找原料单数据
        this.getData()
        //订单编号
        this.$axios.get("/secondPro/OrderNo").then(res => {
            console.log(res.data.data);
            this.allno = res.data.data
        })

    },
    methods: {
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection);
        },
        //分页
        handleSizeChange(val) {
            //改变页面数据条数时的事件
            this.size = val
            this.getData(val)
        },
        handleCurrentChange(val) {
            //改变页数时的事件
            this.page = val
            this.getData(val)
        },
        getData() {
            this.$axios
                .get("/secondPro/findAll/" + this.page + "/" + this.size)
                .then((res) => {
                    let resPage = res.data.data;
                    //
                    this.page = resPage.curPage;
                    // this.size = resPage.size;
                    this.total = resPage.total;
                    this.totalPage = resPage.totalPage;
                    this.tableData = resPage.data;
                });
        },
        minute(row) {
            if (row.status == 1) {
                this.ifMinute = 0
            } else {
                this.ifMinute = 1;
            }
            this.minuteVisible = true;
            console.log("查看详情" + row);
            this.oid = row
            this.$axios.get("/orderItem/findByOid/" + this.oid).then(res => {
                this.goodsData = res.data.data
            })
        },
        isMinute() {
            this.ifMinute = 0
            this.minuteVisible = true;
        },
        //多条件查询
        find() {
            console.log("这是formData的值：" + JSON.stringify(this.formData));
            this.$axios.post("/secondPro/findterm/" + this.page + "/" + this.size, this.formData).then(res => {
                let resPage = res.data.data;
                //
                this.page = resPage.curPage;
                // this.size = resPage.size;
                this.total = resPage.total;
                this.totalPage = resPage.totalPage;
                this.tableData = resPage.data;
            })
        },
        //清空
        empty() {
            this.formData = {}
            this.getData()
        },
        //发货
        sendPass(row) {
            console.log(row.id + "/" + row.status);
            this.$axios.post("/secondPro/upStatus/" + row.id + "/" + row.status + "/" + row.num + "/" + row.productid).then(res => {
                console.log("查看详情" + this.oid);
                this.minute(this.oid)
                // console.log("发送成功" + this.oid);
            })
        },
        allSend() {
            this.multipleSelection.forEach(item => {
                this.status.push(item.status);
            })
            console.log(this.status);
            if (this.status.includes(1)) {
                this.$message({
                    message: '包含已经发货的,请重新选择',
                    type: 'warning'
                });
                this.status = []
            } else {
                this.$axios.post("/secondPro/allSend", this.multipleSelection).then(res => {
                    this.minute(this.oid)
                })
            }
        },
        pass(row) {
            this.$axios.post("/secondPro/isSend/" + row.id).then(res => {
                //修改商品状态
                this.$axios.post("/completePro/passSend/" + row.orderid).then(res => {
                    })
                   
                //应收款
                this.formData = row
                    console.log("这是formData的值：" + JSON.stringify(this.formData));
                    this.$axios.post("/completePro/addReceive",this.formData).then(res =>{
                    })
                    this.formData = {}
                this.getData()
                // console.log("发送成功" + this.oid);
            })
        },
        changeing() {
            this.$axios.get("/orderItem/findByOid/" + this.formData.no).then(res => {
                this.goodsData = res.data.data
            })
        },
        openAdd() {
            this.dialogVisible = true
            //可以添加的订单编号
            this.$axios.get("/completePro/orderNo").then(res => {
                this.orderNo = res.data.data
            })
        },
        //添加
        add() {
            console.log("这是formData的值：" + JSON.stringify(this.formData));
            this.$axios.post("/secondPro/add/" + this.formData.no).then(res => {
                if (res.data.data = true) {
                    this.dialogVisible = false;
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    });
                    this.formData = {};
                } else {
                    this.$message({
                        message: '添加失败',
                        type: 'warning'
                    });
                }
            })
            this.getData()
        },
        deleteItem(row) {
            this.$axios.delete("/completePro/delete/" + row.id).then(res => {
                this.$message({
                    message: '删除成功',
                    type: 'warning'
                });
            })
            this.getData()
        },

    },
}
</script>

<style scoped></style>